package com.borrow.jiemihua.mainUI.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.base.BaseConstant;
import com.borrow.jiemihua.base.BaseRVAdapter;
import com.borrow.jiemihua.base.BaseViewHolder;
import com.borrow.jiemihua.base.URLConstant;
import com.borrow.jiemihua.bean.ApplyAmountBean;
import com.borrow.jiemihua.bean.PlatformDetailsBean;
import com.borrow.jiemihua.utils.SharedPreferencesUtils;
import com.borrow.jiemihua.utils.xutils3.MyCallBack;
import com.borrow.jiemihua.utils.xutils3.XUtil;
import com.borrow.jiemihua.view.dialogView.BaseBottomView;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cc.duduhuo.custoast.CusToast;

@ContentView(R.layout.activity_apply_now)
public class ApplyNowActivity extends BaseActivity {
    @ViewInject(R.id.tv_top_title)
    private TextView tv_top_title;

    @ViewInject(R.id.et_name)
    private EditText et_name;
    @ViewInject(R.id.et_code)
    private EditText et_code;
    @ViewInject(R.id.tv_dkyt)
    private TextView tv_dkyt;
    @ViewInject(R.id.et_sqje)
    private EditText et_sqje;
    @ViewInject(R.id.tv_dkqx)
    private TextView tv_dkqx;
    @ViewInject(R.id.tv_zylb)
    private TextView tv_zylb;
    private String money;
    private String time;
    private String qiTime;
    private String url,name;
    private ApplyAmountBean.DataBean amountBean=new ApplyAmountBean.DataBean();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initData();
        initView();
        upPTDetailsData();
    }

    @Override
    protected void initData() {
        tv_top_title.setText("申请人资质");
        money=getIntent().getStringExtra("money");
        time=getIntent().getStringExtra("time");
        qiTime=getIntent().getStringExtra("qiTime");
        url=getIntent().getStringExtra("url");
        name=getIntent().getStringExtra("name");
    }
    @Override
    protected void initView() {
        et_sqje.setText(money);
        tv_dkqx.setText(time);

    }
    private List listTime;
    private List<String> list=new ArrayList<>();
    private  void addTime(){
        String[] arr = qiTime.split(",");//分割字符串得到数组
        listTime = java.util.Arrays.asList(arr);//字符数组转list
    }
    private void initsetJEBX(final int type){
        final BaseBottomView bottomView = new BaseBottomView(ApplyNowActivity.this, R.layout.platform_details_time);
        bottomView.setCancelable(true);
        bottomView.show();
        RecyclerView recycler_view= (RecyclerView) bottomView.findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        if(type==1){
            list.clear();
            list.add("旅游消费");
            list.add("助学进修");
            list.add("购车消费");
            list.add("婚庆服务");
            list.add("医疗服务");
            list.add("装修建材");
            list.add("百货消费");
            listTime=list;
        }else if(type==2){
            addTime();
        }else if(type==3){//职业类别3
            list.clear();
            list.add("上班族");
            list.add("个体户");
            list.add("无固定职业");
            list.add("企业主");
            list.add("学生");
            listTime=list;
        }

        recycler_view.setAdapter(new BaseRVAdapter(ApplyNowActivity.this,listTime) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.platform_time_item;
            }
            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                if(type==1){ //贷款用途1
                    holder.getTextView(R.id.tv_time_litem).setText(listTime.get(position)+"");
                }else if(type==2){//贷款期限2
                    holder.getTextView(R.id.tv_time_litem).setText(listTime.get(position)+"天");
                }else if(type==3){//职业类别3
                    holder.getTextView(R.id.tv_time_litem).setText(listTime.get(position)+"");
                }
                holder.getTextView(R.id.tv_time_litem).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(type==1){ //贷款用途1
                            tv_dkyt.setText(list.get(position)+"");
                        }else if(type==2){//贷款期限2
                            tv_dkqx.setText(listTime.get(position)+"天");
                        }else if(type==3){//职业类别3
                            tv_zylb.setText(list.get(position)+"");
                        }
                        bottomView.dismiss();
                    }
                });
            }
        });
        bottomView.findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomView.dismiss();
            }
        });
    }
    @Event(value = {R.id.imgbtn_left,R.id.tv_dkqx,R.id.tv_xyb,R.id.tv_zylb,R.id.tv_dkyt},type= View.OnClickListener.class)
    private void getEvent(View view) {
        switch (view.getId()) {
            case R.id.imgbtn_left:
                finish();
                break;
            //贷款期限2
            case R.id.tv_dkqx:
                initsetJEBX(2);
                break;
            case R.id.tv_xyb:
                initGetData();
                break;
            //职业类别3
            case R.id.tv_zylb:
                initsetJEBX(3);
                break;
            //贷款用途1
            case R.id.tv_dkyt:
                initsetJEBX(1);
                break;
        }
    }
    private void initGetData(){
        String name=et_name.getText().toString().trim();
        String card=et_code.getText().toString().trim();
        String amount=et_sqje.getText().toString().trim();

        if(name.equals("")||name==null){
            CusToast.showToast("请输入姓名");
            return;
        }else  if(card.length()!=18){
            CusToast.showToast("请输入正确的身份证号");
            return;
        } else  if(amount.equals("")||amount==null){
            CusToast.showToast("请输入贷款金额");
            return;
        }else {
            upApplyData();
        }
    }
    private void upApplyData(){
        Map<String,Object> map=new HashMap<>();
        String userid = SharedPreferencesUtils.getString(this, BaseConstant.SPConstant.USERID, "");
        map.put("userId",userid);
        map.put("name",et_name.getText().toString().trim());
        map.put("idCard",et_code.getText().toString().trim());
        map.put("lendPurpose",tv_dkyt.getText().toString().trim());
        map.put("applyForAmount",et_sqje.getText().toString().trim());
        String time =tv_dkqx.getText().toString().trim();
        map.put("timeLimit",time.substring(0,time.length()-1)+"");
        map.put("profession",tv_zylb.getText().toString().trim());
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("---申请贷款参数-" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(URLConstant.APPLY_DAIKUAN,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
//                Log.i("===申请贷款",result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("isSuccess");
                    if(res.equals("1")){
                        startActivity(new Intent(ApplyNowActivity.this,ApplyWebViewActivity.class)
                                .putExtra("name",name)
                                .putExtra("url",url));
//                       CusToast.showToast("已提交申请");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
                closeDialog();
            }
        });
    }
    private void setData(){
        et_name.setText(amountBean.getName());
        et_code.setText(amountBean.getIdCard());
        tv_dkyt.setText(amountBean.getLendPurpose());
        et_sqje.setText(amountBean.getApplyForAmount()+"");
        tv_dkqx.setText(amountBean.getTimeLimit()+"天");
        tv_zylb.setText(amountBean.getProfession());
    }
    private void upPTDetailsData(){
        Map<String,String> map=new HashMap<>();
        String userid = SharedPreferencesUtils.getString(this, BaseConstant.SPConstant.USERID, "");
        map.put("userId",userid);
        showDialog();
        XUtil.Get(URLConstant.GET_JIEKUANXINXI,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==获取借款信息",result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("isSuccess");
                    String data = jsonObject.optString("data");

                    if(res.equals("1")){
                        if(data.equals("")&&data!=null){
                            Gson gson=new Gson();
                            ApplyAmountBean applyAmountBean=gson.fromJson(result,ApplyAmountBean.class);
                            amountBean=applyAmountBean.getData();
                            setData();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
                closeDialog();
            }
        });
    }
}
